#pragma once
#include"Common.hpp"
#include"ThreadCache.hpp"
#include"CentralCache.hpp"
#include"PageCache.hpp"
static void* ConcurrentAlloc(size_t size)
{
    if(size>MAX_BYTES)
    {
        size_t alignSize=SizeClass::Roundup(size);
        size_t kpage=alignSize>>PAGE_SHIFT;
        PageCache::GetInstance()->_pageMtx.lock();
        Span* span=PageCache::GetInstance()->NewSpan(kpage);
        PageCache::GetInstance()->_pageMtx.unlock();
        void* ptr=(void*)(span->_pageId<<PAGE_SHIFT);
        return ptr;
    }
    if(pTLSThreadCache==nullptr)
    {
        pTLSThreadCache=new ThreadCache;
    }
    return pTLSThreadCache->Allocate(size);
}
static void ConcurrentFree(void* ptr,size_t size)
{
    if(size>MAX_BYTES)
    {
        Span* span=PageCache::GetInstance()->MapObjectToSpan(ptr);
        PageCache::GetInstance()->_pageMtx.lock();
        PageCache::GetInstance()->ReleaseSpanToPageCache(span);
        PageCache::GetInstance()->_pageMtx.unlock();
        return;
    }
    assert(pTLSThreadCache);
    pTLSThreadCache->Deallocate(ptr,size);
}